///******************** NG.Respate(C)  **********************************
//* 产品名称: Respate 科技
//* 文件名称: signal.c
//* 程序作者: 雷帕特公司(南宫子萍)
//* 程序版本: V1.1														   
//* 编制日期: 2019/1/29
//* 功能描述: 系统日志
//* QQ:	2085827949
//**********************************************************************/
#include "mangerUser.h"

FlightList* flightList = NULL; //用户链表
FlightList* userFlightList = NULL; //用户航班购买记录链表
//
int strVague(int opt)
{
    int i = 0;
    int len = LinkList_Length(flightList->listHead);
    char *p = NULL;
    Flight* flight = NULL;
    char data[30]={0};
    char buff[2]={0};
    system("clear");
    getScanf("请输包含字符串:",data,30);
    isShowFlightHeadInfo(1);
    for(i=0;i<len;i++)
    {
        flight = (Flight*)LinkList_Get(flightList->listHead,i);
        if(flight!=NULL)
        {   
            if(opt==1) p= flight->stime;    
            else if(opt==2) p= flight->date;
            else if(opt==3){buff[0]=flight->type;p=buff;} 
            else return -1;
            if(strstr(p,data)!=0)
                showeFlightInfo(flight);
        }
    }
    if(p==NULL) printf("无相关航班信息!\r\n");
    return 0;
}



//模糊查询航班
void checkFlightVague(void)
{
#define checkFlightVague_DATA \
"\
==================check===============          \r\n\
            1. 起飞时间                         \r\n\
            2.班期                              \r\n\
            3.机型                              \r\n\
            4.退出                              \r\n\
=======================================         \r\n\
"
    char buff[2]={0};
    
    while(1)
    {
        system("clear");
        isShowFlightHeadInfo(1);        //显示头信息
        printf(checkFlightVague_DATA);  //显示菜单
        getScanf("请输入选择:",buff,2);
        
        switch (buff[0]-'0')
        {
        case 1: strVague(1);     //通过起飞时间查询       
            break;
        case 2: strVague(2);     //通过班期查询    
            break;
        case 3: strVague(3);   //通过机型查询        
            break;

        case 4: return ;    //退出当前循环
        }
        printf("输入\'c\'继续！\r\n");
            while (getchar()!='c');
    }
}


//查询
void checkFlight(int i)
{
    char buff[20]={0};
    Flight* flight = NULL;
    if(i == 1)
    {
        checkFlightVague();     //模糊查询航班
    }
    else
    {
        isShowFlightHeadInfo(1);    //显示头信息
        getScanfClear("请输入要查询的班期：",buff,20);
        flight = getFlight_byName(flightList,buff);
        isNull(flight,"此航班不存在！",);
        showeFlightInfo(flight);
    } 
}


//修改航班
void modefiyFlight(int i)
{
    char buff[100]={0};
    //4.删除航班  -> 必须管理员登录过才能删除，普通用户不能删除航班    -> 对应的文件也要删除
    //5.录入航班  -> 
    tureRootUser(currentUser,"非管理员，禁止此操作！",);
    
    if(i == 1)
    {
        system("clear");
        showeAllFlight(flightList);
        getScanf("请输入要删除的航班号:",buff,20);
        if(deleteFlight_ByName(flightList,buff)>=0)
        printf("删除航班成功!\r\n");
        return;
    }
    else registerFlight(flightList);  //注册航班
}

//买票
int buyTickt(User* user)
{
    char buff[20]={0};
    Flight* flight = NULL;
    Flight* userflight = NULL;
    float price =1;
    errRootUser(user,"非法操作，请使用普通用户操作",);

    // if(user->flight_num[0]!=0)
    // {
    //     printf("你有待飞航班：%s 班期为：%s ",user->flight_num,user->date);
    //     return;
    // }
    system("clear");    //清屏
    showeAllFlight(flightList);   //显示所有航班信息
    getScanf("请输入要购买的航班编号：",buff,20);
    flight = getFlight_byName(flightList,buff);
    isNull(flight,"无此航班，购票失败！",);
    if(user->isVip>=1) price = 0.95;
    price*=flight->price;

    if(user->balance>=(int)(price)) user->balance -=(int)price;
    else 
    { isNull(NULL,"余额不足，购票失败！",);}
    
    strcpy(user->flight_num,flight->number);
    strcpy(user->date,flight->date);
    printf("用户：%s 购买 %s号航班成功！\r\n",user->name,user->flight_num);
    userSave();

    //保存购买数据到用户记录
    userflight = (Flight*) calloc(1,sizeof(Flight));
    memcpy(userflight,flight,sizeof(Flight));
    userflight->owenr = userFlightList;
    addFlight(userflight);
    flightSave(userFlightList);
    return 0;
}

//退票
int reTickt(User* user)
{
    char buff[102]={0};
    Flight* flight = NULL;
   if(getFilghtNumber(userFlightList)<=0)
   {
        printf("尚未购票，操作失败！\r\n");
        return -1;
   }
    showeAllFlight(userFlightList);
    getScanf("请输入班期号：",buff,10);
    flight = getFlight_byName(userFlightList,buff);
    isNull(flight,"未知机型，请联系人工服务处理！",-1);
    user->balance+=(int)(flight->price *0.05);
    deleteFlight(flight);
    //flightSave(userFlightList);
    userSave(); //保存用户信息
    return 0;
}


void tickeModefiy(int i)
{
    // 6.购票   -> 只有普通用户才能买票   (买的票，可以将这个票的航班号写入文件保存)     -> 链表/文件
    // 7.查询自己买了的票
    // 8.退票    -> (扣手续费) 扣5%    -> (加时间判断)
    // 9.改签
    char buff[20]={0};
    Flight* flight = NULL;
    User* user = NULL;
    int price =0;
    int tempPrice =0;
    errRootUser(currentUser,"非法操作，请使用普通用户操作",);
    if(i==1)    //购票
    {   
        buyTickt(currentUser);
    }
    else if(i==2)//查询自己买了的票
    {
    
        if(getFilghtNumber(userFlightList)<=0) printf("尚无购票记录！\r\n");
        else    showeAllFlight(userFlightList);
    }
    else if(i==3)//退票
    {
        printf("退票请求正在处理中ing....\r\n");
        if(reTickt(currentUser)<0)    isNull(NULL,"退票失败！",-1);
        isNull(NULL,"退票成功！",-1);
    }
    else if(i==4)//改签
    {
        //得到票价
        flight = getFlight_byName(flightList,currentUser->flight_num);
        isNull(flight,"未知机型，请联系人工服务处理！",-1);
        tempPrice = price=flight->price;
        price+=currentUser->balance;

        user = (User*)calloc(1,sizeof(User));
        memcpy(user,currentUser,sizeof(User));

         //退票
        if(reTickt(user)<0)
            goto err1;

        user->balance = price;
        if (buyTickt(user)<0)
            goto err1;

        flight = getFlight_byName(flightList,user->flight_num);
        tempPrice -=flight->price;

        if(tempPrice>0) printf("改签存在差价，已归还差价：%d\r\n",tempPrice);
        else if(tempPrice<0) printf("改签存在差价，已补差价：%d\r\n",-tempPrice);
        user->balance +=(int)tempPrice;

        memcpy(currentUser,user,sizeof(User));
        userSave();
        free(user);
        isNull(NULL,"改签成功！",-1);

        err1:
        free(user);
        isNull(NULL,"改签失败！",-1);
    }


}


//修改用户
void userModefiy(int i)
{
    // 10. 充值
    // 11. 查询自己的余额
    // 12. vip  -> 只需要充值100块，填写个人信息就可以变成vip    冲100送20，买票打95折
    // 13. 修改个人信息： 修改自己的名字，密码等
    switch (i)
    {
        case 1:Recharge(currentUser);       //充值金额
        break;
        case 2:printf("用户:%s\r\n当前余额:%d￥\r\n"    \
                ,currentUser->name,currentUser->balance);//余额查询
        break;
        case 3:RechargeVip(currentUser);       //升级VIP     
        break;
        case 4:chengUser(currentUser);      //修改用户信息
        break;
        default:
        break;
    }
     
}









#define MAIN_UI \
"\
================用户:%s=================   \r\n\
		1. 显示所有的航班                    \r\n\
		2. 查询                              \r\n\
		3. 快速查询                          \r\n\
		4. 删除航班                          \r\n\
		5. 录入航班                          \r\n\
		6. 购票                              \r\n\
		7. 查询自己买了的票                   \r\n\
		8. 退票                              \r\n\
		9. 改签                              \r\n\
		10. 充值                            \r\n\
		11. 查询自己的余额                   \r\n\
		12. vip                             \r\n\
		13. 修改个人信息                    \r\n\
		14. 退出系统                        \r\n\
==========================================  \r\n\
"
int mainUiI(void)
{
    char buff[2]={0};
    int i=0;
    char userFly[1024]={0};
    //初始化航班信息
    flightList = flightInit(CFG_FLY_DATA_DIR""CFG_FLY_DATA);

    sprintf(userFly,"%s/%s",CFG_FLY_DATA_DIR,currentUser->number);
    userFlightList = flightInit(userFly);
    while(1)
    {
        system("clear");
        printf(MAIN_UI,currentUser->name);
        getScanf("请输入选择:",buff,3);
    
        if (buff[1]) i =10+buff[1]-'0';
        else i = buff[0]-'0';
        
        switch (i)
        {
        case 1: showeAllFlight(flightList);   //显示所有的航班
            break;
        case 2: checkFlight(1);     //查询       
                //system("clear");
            break;
        case 3: checkFlight(2);     //快速查询
                //system("clear");
            break;
        
        case 4: modefiyFlight(1);   //删除航班       
            break;
        case 5: modefiyFlight(2);   //录入航班
            break;

        case 6: tickeModefiy(1);    //购票       
            break;
        case 7: tickeModefiy(2);    //查询自己买了的票
            break;
        case 8: tickeModefiy(3);    //退票       
            break;
        case 9: tickeModefiy(4);    //改签
            break;
        
        case 10: userModefiy(1);    //充值
            break;
        case 11: userModefiy(2);    // 查询自己的余额      
            break;
        case 12: userModefiy(3);    //vip
            break;
        case 13: userModefiy(4);    //修改个人信息
            break;

        case 14: exitSystem();      //退出系统       
            break;

        default:
            break;
        }
        printf("输入\'c\'继续！\r\n");
            while (getchar()!='c');
    }
    return 0;
    
}



																						 
